
package controller;

import java.io.IOException;
import java.util.List;
import javax.faces.context.FacesContext;
import model.dao.ImpedimentDao;
import model.domain.Impediment;
import model.domain.Project;


public class impedimentBean {

    public impedimentBean() {
        newimpediment = new Impediment();
        selectedimpediment = new Impediment();
    }

    private Project project;
    private Impediment newimpediment;
    private Impediment selectedimpediment;

    public void setProject(Project object){
        this.project = object;
    }

    public Project getProject(){
	return this.project;
    }

    public void setNewimpediment(Impediment impediment){
        this.newimpediment = impediment;
    }

    public Impediment getNewimpediment(){
	return this.newimpediment;
    }

    public void setSelectedimpediment(Impediment impediment){
        this.selectedimpediment.setImpedimentID(impediment.getImpedimentID());
        this.selectedimpediment.setDescription(impediment.getDescription());
        this.selectedimpediment.setComments(impediment.getComments());
        this.selectedimpediment.setDay(impediment.getDay());
        this.selectedimpediment.setProject(this.project);
        this.selectedimpediment.setStatus(impediment.getStatus());
        this.selectedimpediment.setUser(impediment.getUser());
    }

    public Impediment getSelectedimpediment(){
	return this.selectedimpediment;
    }

    public List<Impediment> getImpedimentsByProject(){
        ImpedimentDao home = new ImpedimentDao();
        List<Impediment> list = home.findByProject(this.project);
        return list;
    }

    public void submitCreate() throws IOException {

        ImpedimentDao home = new ImpedimentDao();

        Impediment auxImpediment = new Impediment();
        auxImpediment.setDescription(this.newimpediment.getDescription());
        auxImpediment.setComments(this.newimpediment.getComments());
        auxImpediment.setProject(this.project);
        auxImpediment.setDay(this.newimpediment.getDay());
        auxImpediment.setStatus(this.newimpediment.getStatus());
        auxImpediment.setUser(this.newimpediment.getUser());

        home.insert(auxImpediment);

        this.newimpediment.setDescription("");
        this.newimpediment.setComments("");
        this.newimpediment.setDay(null);
        this.newimpediment.setStatus(null);
        this.newimpediment.setUser(null);

        FacesContext.getCurrentInstance().getExternalContext().redirect("Impediments.xhtml");
    }

    public void submitEdit(){

        ImpedimentDao home = new ImpedimentDao();
        home.insertOrUpdate(this.selectedimpediment);
    }

    public void submitDelete(){
        ImpedimentDao home = new ImpedimentDao();
        home.delete(this.selectedimpediment);
    }
}
